import 'package:flutter/material.dart';
import 'package:read_book/data/book_mall/book_mall.dart';
import 'package:read_book/ui/pages/book_info/book_content.dart';

class Catalog extends StatefulWidget {
  String bookId;

  Catalog({Key key, this.bookId}) : super(key: key);

  @override
  CatalogState createState() => CatalogState();
}

class CatalogState extends State<Catalog> {
  List bookList = [];

  void getBookCatalog() {
    BookMall().getBookCatalog(widget.bookId).then((value) {
      setState(() {
        bookList = value['mixToc']['chapters'];
      });
    });
  }

  @override
  void initState() {
    getBookCatalog();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 280,
      color: Colors.grey[50],
      child: ListView(
        children: <Widget>[
          Container(
            height: 40.0,
            width: double.infinity,
            decoration: const BoxDecoration(
                border: Border(bottom: BorderSide(color: Colors.black12))),
            child: GestureDetector(
              onTap: () {
                debugPrint('排序');
              },
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: const <Widget>[
                  Text(
                    '目录',
                    style: TextStyle(color: Colors.black87, fontSize: 180),
                    textAlign: TextAlign.center,
                  ),
                  Icon(
                    Icons.import_export,
                    color: Colors.black54,
                  )
                ],
              ),
            ),
          ),
          Container(
            child: buildList(context),
          )
        ],
      ),
    );
  }

  //目录列表
  Widget buildList(BuildContext context) {
    return ListView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemCount: bookList.length,
      itemBuilder: (BuildContext context, int index) {
        return Container(
          decoration: BoxDecoration(
              border: Border(bottom: BorderSide(color: Colors.grey[200]))),
          height: 40.0,
          child: GestureDetector(
            onTap: () {
              Navigator.pop(context);
              Navigator.push(context, MaterialPageRoute(builder: (_) {
                return BookContent(
                    bookLink: bookList[index]['link'], bookId: widget.bookId);
              }));
            },
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Container(
                  width: 220.0,
                  padding: const EdgeInsets.only(left: 15.0),
                  child: Text.rich(
                    TextSpan(
                        text: '${index + 1}',
                        style: const TextStyle(
                            color: Colors.black54, fontSize: 12.0),
                        children: <TextSpan>[
                          TextSpan(
                              text: '${bookList[index]['title']}',
                              style: const TextStyle(
                                  color: Colors.black54, fontSize: 14.0))
                        ]),
                    softWrap: false,
                    textAlign: TextAlign.left,
                    overflow: TextOverflow.ellipsis,
                    maxLines: 1,
                  ),
                ),
                Container(
                  padding: const EdgeInsets.only(right: 15.0),
                )
              ],
            ),
          ),
        );
      },
    );
  }
}
